import matplotlib.pyplot as plt
import numpy as np

def plot_comparison(dda_points, bresenham_points, title):
    x_min = min(min(x for x, y in dda_points), min(x for x, y in bresenham_points)) - 1
    x_max = max(max(x for x, y in dda_points), max(x for x, y in bresenham_points))
    y_min = min(min(y for x, y in dda_points), min(y for x, y in bresenham_points)) - 1
    y_max = max(max(y for x, y in dda_points), max(y for x, y in bresenham_points))

    plt.figure(figsize=(16, 8))

    # 左图：DDA算法
    plt.subplot(1, 2, 1)
    for x, y in dda_points:
        plt.gca().add_patch(plt.Rectangle((x - 0.5, y - 0.5), 1, 1, color='#a5cad2'))
    plt.xlim(x_min, x_max)
    plt.ylim(y_min, y_max)
    plt.xticks(range(x_min, x_max + 1))
    plt.yticks(range(y_min, y_max + 1))
    plt.minorticks_on()
    plt.gca().set_xticks(np.arange(x_min, x_max + 1, 0.5), minor=True)
    plt.gca().set_yticks(np.arange(y_min, y_max + 1, 0.5), minor=True)
    plt.grid(True, which='major', color='gray', linewidth=0.5, linestyle='-', alpha=0.7)
    plt.grid(True, which='minor', color='gray', linewidth=0.3, linestyle='--', alpha=0.5)
    plt.gca().set_aspect('equal', adjustable='box')
    plt.title("DDA Line")
    plt.xlabel('X Axis')
    plt.ylabel('Y Axis')

    # 右图：Bresenham算法
    plt.subplot(1, 2, 2)
    for x, y in bresenham_points:
        plt.gca().add_patch(plt.Rectangle((x - 0.5, y - 0.5), 1, 1, color='#ff7b89'))
    plt.xlim(x_min, x_max)
    plt.ylim(y_min, y_max)
    plt.xticks(range(x_min, x_max + 1))
    plt.yticks(range(y_min, y_max + 1))
    plt.minorticks_on()
    plt.gca().set_xticks(np.arange(x_min, x_max + 1, 0.5), minor=True)
    plt.gca().set_yticks(np.arange(y_min, y_max + 1, 0.5), minor=True)
    plt.grid(True, which='major', color='gray', linewidth=0.5, linestyle='-', alpha=0.7)
    plt.grid(True, which='minor', color='gray', linewidth=0.3, linestyle='--', alpha=0.5)
    plt.gca().set_aspect('equal', adjustable='box')
    plt.title("Bresenham Line")
    plt.xlabel('X Axis')
    plt.ylabel('Y Axis')

    plt.suptitle(title)
    plt.show()
